package com.iemes.controller.warehouse;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.EnterstorageFormMap;
import com.iemes.entity.OrderperFormMap;
import com.iemes.entity.OutreceiptFormMap;
import com.iemes.entity.PcontractTrackingDetailFormMap;
import com.iemes.entity.ProductreturnFormMap;
import com.iemes.entity.TransportTransporinfoFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.shape.ShapeMapper;
import com.iemes.mapper.transport.TransporinfoMapper;
import com.iemes.mapper.warehouse.EnterstorageMapper;
import com.iemes.mapper.warehouse.OutreceiptMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.MyDateUtils;

@RequestMapping("/outreceipt_all/")
@Controller
public class Outreceipt_AllController extends BaseController {

	@Inject
	private EnterstorageMapper enterstorageMapper;

	@Inject
	private TransporinfoMapper transporinfoMapper;
	
	@Inject
	private OutreceiptMapper outreceiptMapper;
	
	@Inject
	private ShapeMapper shapeMapper;

	// 查看车辆调度明细表，显示按钮
	@RequestMapping("outreceipt_all")
	public String outreceipt_all(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/warehouse/outreceipt_all_list";
	}

	// 车辆调度明细表分页
	@ResponseBody
	@RequestMapping("outreceipt_all_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		TransportTransporinfoFormMap transporinfoFormMap = getFormMap(TransportTransporinfoFormMap.class);
		List<Map<String, Object>> transport = transporinfoMapper.getAll(transporinfoFormMap);
		LuogicPaging(pageNow, pageSize, transport);
		return pageView;
	}

	// 查看编辑出仓单
	@RequestMapping("outreceipt_all_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if (Common.isNotEmpty("id")) {
			// 根据Id获得车辆调度明细表
			TransportTransporinfoFormMap tInfo = transporinfoMapper.getById(id);
			model.addAttribute("tInfo", tInfo);
			// 获得相同tInfoId的出仓单
			OutreceiptFormMap outreceiptFormMap = new OutreceiptFormMap();
			outreceiptFormMap.put("tInfoId", tInfo.get("id"));
			List<OutreceiptFormMap> receipt = baseMapper.findByNames(outreceiptFormMap);
			model.addAttribute("receipt", receipt);
		}
		return Common.BACKGROUND_PATH + "/warehouse/outreceipt_all_edit";
	}

	// 查询所有当天的出货计划
	@ResponseBody
	@RequestMapping("getOrderPlanFindByPage")
	public PageView getOrderPlanFindByPage(String pageNow, String pageSize) throws Exception {
		EnterstorageFormMap enterstorageFormMap = new EnterstorageFormMap();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		enterstorageFormMap.put("material_time", sdf.format(date));
		List<Map<String, Object>> list = enterstorageMapper.getTodayOrder(enterstorageFormMap);
		LuogicPaging(pageNow, pageSize, list);
		return pageView;
	}
	
	//删除出仓单
	@ResponseBody
	@RequestMapping(value="outreceipt_all_deleteEntity")
	@Transactional(readOnly = false) // 需要事务操作必须加入此注解
	@SystemLog(module = "编辑出仓单", methods = "出仓单-删除出仓单")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity(HttpServletRequest request) throws Exception {
		String id = getPara("pid");
		String tid=getPara("tid");
		int eid=Integer.parseInt(getPara("eid"));
		OutreceiptFormMap outreceipt=outreceiptMapper.getById(id);
		int outNumber=Integer.parseInt(outreceipt.get("outNumber").toString());
		TransportTransporinfoFormMap transport=transporinfoMapper.getById(tid);
		int outNumberTotal=Integer.parseInt(transport.get("outNumberTotal").toString());
		//计算车辆调度表出货数量总数
		transport.put("outNumberTotal", outNumberTotal-outNumber);
		baseMapper.editEntity(transport);
		EnterstorageFormMap enter=enterstorageMapper.getEnterstorangByid(eid);
		double outTotal=Double.parseDouble(enter.get("outTotal").toString());
		double orderPack=Double.parseDouble(enter.get("orderPack").toString());
		//计算入库报表出货件数
		enter.put("outTotal", outTotal-outNumber);
		//计算入库报表出货包数
		enter.put("outPack", (outTotal-outNumber)/orderPack);
		baseMapper.editEntity(enter);
		//把erp_orderpre_list表的state字段改为4
		String orderId=enter.get("orderId").toString();
		System.out.println("orderId："+orderId);
		OrderperFormMap orderper=shapeMapper.getByOrderId(orderId);
		orderper.put("state", 4);
		baseMapper.editEntity(orderper);
		baseMapper.deleteByAttribute("id", id, OutreceiptFormMap.class);
		baseMapper.deleteByAttribute("outReceiptId", id, ProductreturnFormMap.class);
		return "success";
	}

	// 修改出仓单
	@ResponseBody
	@RequestMapping(value = "outreceipt_all_editEntity")
	@Transactional(readOnly = false) // 需要事务操作必须加入此注解
	@SystemLog(module = "编辑出仓单", methods = "出仓单-修改出仓单") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws NumberFormatException, Exception {
		TransportTransporinfoFormMap transporinfoFormMap = getFormMap(TransportTransporinfoFormMap.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		// 获取request
		request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		// 新增标签子项
		int tInfoId = Integer.parseInt(request.getParameter("transportTransporinfoFormMap.id"));
		// 计算“出货数量总数”
		int total1 = 0;
		int total2 = 0;
		int i = 0;
		String[] outNumber1 = request.getParameterValues("outNumber_1");
		String[] eid1=request.getParameterValues("eid_1");
		String[] contract_num2 = request.getParameterValues("contract_num_2");
		String[] oid2 = request.getParameterValues("oid_2");
		String[] material_code2 = request.getParameterValues("material_code_2");
		String[] warehouse2 = request.getParameterValues("warehouse_2");
		String[] salesname2 = request.getParameterValues("salesname_2");
		String[] mold_code2 = request.getParameterValues("mold_code_2");
		String[] outNumber2 = request.getParameterValues("outNumber_2");
		String[] productName2 = request.getParameterValues("productName_2");
		String[] eid2 = request.getParameterValues("eid_2");
		if (null != outNumber1) {
			for (i = 0; i < outNumber1.length; i++) {
				total1 += Integer.parseInt(outNumber1[i]);
				double outTotal=0,outPack=0;
				List<EnterstorageFormMap> enter=enterstorageMapper.getByEid(eid1[i]);
				List<OutreceiptFormMap> receipt = outreceiptMapper.getAllOutNumber(eid1[i]);
				for(EnterstorageFormMap e:enter){
					for(OutreceiptFormMap r:receipt){
						if(e.get("id").toString().equals(r.get("eid").toString())){
							int outNumber=Integer.parseInt(r.get("outNumber").toString());
							double orderPack=Double.parseDouble(e.get("orderPack").toString());
							//计算每种物料的出货件数
							outTotal+=outNumber;
							//计算每种物料的出货包数
							outPack=outTotal/orderPack;
						}
					}
				}
				for(EnterstorageFormMap map:enter){
					map.put("outTotal", outTotal);
					map.put("outPack", outPack);
					baseMapper.editEntity(map);
				}
			}
			transporinfoFormMap.put("formCategory", 1);
			transporinfoFormMap.put("warehouseKeeperId", userFormMap.getInt("id"));
			transporinfoFormMap.put("outNumberTotal", total1);
			baseMapper.editEntity(transporinfoFormMap);
		}
		if (null != material_code2) {
			for (i = 0; i < material_code2.length; i++) {
				OutreceiptFormMap outreceiptFormMap = new OutreceiptFormMap();
				outreceiptFormMap.put("tInfoId", tInfoId);
				outreceiptFormMap.put("contract_num", contract_num2[i]);
				outreceiptFormMap.put("oid", oid2[i]);
				outreceiptFormMap.put("material_code", material_code2[i]);
				outreceiptFormMap.put("warehouse", warehouse2[i]);
				outreceiptFormMap.put("salesname", salesname2[i]);
				outreceiptFormMap.put("mold_code", mold_code2[i]);
				outreceiptFormMap.put("outNumber", outNumber2[i]);
				if (!outNumber2[i].isEmpty()) {
					total2 += Integer.parseInt(outNumber2[i]);
				}
				outreceiptFormMap.put("productName", productName2[i]);
				outreceiptFormMap.put("eid", eid2[i]);
				baseMapper.addEntity(outreceiptFormMap);
				double outTotal=0,outPack=0;
				List<EnterstorageFormMap> enter=enterstorageMapper.getByEid(eid2[i]);
				List<OutreceiptFormMap> receipt = outreceiptMapper.getAllOutNumber(eid2[i]);
				for(EnterstorageFormMap e:enter){
					for(OutreceiptFormMap r:receipt){
						if(e.get("id").toString().equals(r.get("eid").toString())){
							int outNumber=Integer.parseInt(r.get("outNumber").toString());
							double orderPack=Double.parseDouble(e.get("orderPack").toString());
							//计算每种物料的出库数量
							outTotal+=outNumber;
							//计算每种物料的出货包数
							outPack=outTotal/orderPack;
						}
					}
				}
				for(EnterstorageFormMap map:enter){
					map.put("outTotal", outTotal);
					map.put("outPack", outPack);
					baseMapper.editEntity(map);
					//把erp_orderpre_list表的state字段改为3
					String orderId=map.get("orderId").toString();
					List<OrderperFormMap> orderper=shapeMapper.getByOid(orderId);
					for(OrderperFormMap map2:orderper){
						map2.put("state", 3);
						baseMapper.editEntity(map2);
					}
				}
				ProductreturnFormMap productreturnFormMap=getFormMap(ProductreturnFormMap.class);
				productreturnFormMap.put("outReceiptId", outreceiptFormMap.get("id"));
				baseMapper.addEntity(productreturnFormMap);
			}
			String date1 = request.getParameter("transportTransporinfoFormMap.receiptInterval");
			transporinfoFormMap.put("noReturnTime", MyDateUtils.intervalDate(sdf.format(date), date1));
			transporinfoFormMap.put("outDate", sdf.format(date));
			transporinfoFormMap.put("formCategory", 1);
			transporinfoFormMap.put("warehouseKeeperId", userFormMap.getInt("id"));
			transporinfoFormMap.put("outNumberTotal", total1+total2);
			baseMapper.editEntity(transporinfoFormMap);
		}
		if(null == outNumber1 && null == material_code2){
			transporinfoFormMap.put("formCategory", 0);
			transporinfoFormMap.put("warehouseKeeperId", userFormMap.getInt("id"));
			transporinfoFormMap.put("outNumberTotal", 0);
			baseMapper.editEntity(transporinfoFormMap);
		}
		return "success";
	}
}


